import requests
import re
import argparse
import sys
import json
import os
from scrapy.selector import Selector
from tqdm import tqdm
from pptx import Presentation
import time
from pptx.util import Inches

parser = argparse.ArgumentParser()
parser.add_argument("url", help="Target Url,你所需要文档的URL",type=str)
args = parser.parse_args()

url = args.url


def PDF(url):

    html = requests.get(url,headers=headers,cookies=cookies).text
    title = Selector(text=html).css('.doc-title::text').get()
    lists = Selector(text=html).css('img::attr(src)').getall()
    lists_2 = Selector(text=html).css('img::attr(data-src)').getall()
    lists.extend(lists_2)
    tqdm.write("=========================={脚本启动}==========================")
    time.sleep(0.3)
    tqdm.write("最后一次更新脚本：{}".format("2022-06-07"))
    time.sleep(0.3)
    tqdm.write("Version：{}".format("1.0.0"))
    time.sleep(0.3)
    tqdm.write("URL：{}".format(url))
    time.sleep(0.3)
    tqdm.write("标题：{}".format(title))
    time.sleep(0.3)
    tqdm.write("=============================================================")
    time.sleep(0.5)
    tqdm.write("正在拉取图片请耐心等待...")
    try:
        os.mkdir(title)
    except:
        pass
    for i in tqdm(range(0,len(lists))):
        with open("{}/{:0>3d}.jpg".format(title,i+1), 'wb') as m:
            m.write(requests.get(lists[i]).content)
    tqdm.write("PPT图片保存在：「{}/{}」文件夹".format(os.getcwd(),title))
    return title

def combin(path):
    tqdm.write("=========================={}==========================".format("开始合成PPT"))
    prs = Presentation()
    if os.path.exists("{}/.DS_Store".format(path)):
        os.remove("{}/.DS_Store".format(path))
    files = os.listdir(path)
    files.sort(key=lambda x: int(x[:-4]))
    for file in tqdm(files):
        blank_slide_layout = prs.slide_layouts[0]
        slide = prs.slides.add_slide(blank_slide_layout)
        pic = slide.shapes.add_picture("{}/{}".format(path,file), 0, 0)
    name = '{}.pptx'.format(path)
    prs.save(name)
    return name

if __name__ == "__main__":
    u = "https://wenku.baidu.com/view/2c4967b8dd36a32d72758195.html?fr=income1-wk_app_search_ctr-search"
    u2 = "https://wenku.baidu.com/view/0afda52abcd126fff7050b7f.html"
    u3 = "https://wenku.baidu.com/view/802fd575951ea76e58fafab069dc5022abea4632.html?re=view"
    cookies = {
        'BAIDUID': '2D50A344EB631A810A4BB413B3F0DF0F:FG=1',
        'BAIDUID_BFESS': '2D50A344EB631A810A4BB413B3F0DF0F:FG=1',
        'ab_sr': '1.0.1_YTZiMzNjMjhkMzQ4ZjE0MmQwMTk2ZGUxMjhiNWUyMTZjMjNmMDFkZDg4NDI5Yzc2NzgzZGE3M2E2NGMzZWYxMjU4ZTg1ZTRhZmFkMDZmZjFmMzg5OTZlNzlkMjZiZDk2YTU0MDNkYjZkNjA1MWMwMWIyMmE0ODcyNzdhM2QxNDg0ODlkN2Y1NTU2NTUyNjU0ZTAyZGMwYWZkNjc5NzYzMg==',
        'BDUSS': 'FKejY4aGJJUklVSUF1ZjBwalpxSldOY1dYMEU0ODFOZUlhfnNRbWp-eVdDOFppRVFBQUFBJCQAAAAAAAAAAAEAAABHZsMYd3o1NzIyMjAyMTYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJZ-nmKWfp5iTj',
        'BDUSS_BFESS': 'FKejY4aGJJUklVSUF1ZjBwalpxSldOY1dYMEU0ODFOZUlhfnNRbWp-eVdDOFppRVFBQUFBJCQAAAAAAAAAAAEAAABHZsMYd3o1NzIyMjAyMTYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJZ-nmKWfp5iTj',
    }

    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Language': 'en-US,en;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        # Requests sorts cookies= alphabetically
        # 'Cookie': 'BAIDUID=2D50A344EB631A810A4BB413B3F0DF0F:FG=1; BAIDUID_BFESS=2D50A344EB631A810A4BB413B3F0DF0F:FG=1; ab_sr=1.0.1_YTZiMzNjMjhkMzQ4ZjE0MmQwMTk2ZGUxMjhiNWUyMTZjMjNmMDFkZDg4NDI5Yzc2NzgzZGE3M2E2NGMzZWYxMjU4ZTg1ZTRhZmFkMDZmZjFmMzg5OTZlNzlkMjZiZDk2YTU0MDNkYjZkNjA1MWMwMWIyMmE0ODcyNzdhM2QxNDg0ODlkN2Y1NTU2NTUyNjU0ZTAyZGMwYWZkNjc5NzYzMg==; BDUSS=FKejY4aGJJUklVSUF1ZjBwalpxSldOY1dYMEU0ODFOZUlhfnNRbWp-eVdDOFppRVFBQUFBJCQAAAAAAAAAAAEAAABHZsMYd3o1NzIyMjAyMTYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJZ-nmKWfp5iTj; BDUSS_BFESS=FKejY4aGJJUklVSUF1ZjBwalpxSldOY1dYMEU0ODFOZUlhfnNRbWp-eVdDOFppRVFBQUFBJCQAAAAAAAAAAAEAAABHZsMYd3o1NzIyMjAyMTYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJZ-nmKWfp5iTj',
        'Origin': 'https://wenku.baidu.com',
        'Pragma': 'no-cache',
        'Referer': 'https://wenku.baidu.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"macOS"',
    }
    tqdm.write("🎉已完成,文件保存在「{}/{}」".format(os.getcwd(),combin(PDF(url))))
    tqdm.write("=========================={}==========================".format("🔘"))

    print("""
###Author:Ao
###Version：V1.0.0
###LastTimeUpdate:2022-06-07
###Blog:https://www.roguelife.de
###TIPS:当前只支持下载百度文库中的PPT
    """)
